﻿/*
 This file was create by Xusion at 01.06.2011
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace WeiBo.SinaAPI
{
    public static class UserController
    {
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="xmlUser"></param>
        /// <returns></returns>
        public static SinaMUsers GetModelByUserXml(string xmlUser)
        {
            if (!string.IsNullOrEmpty(xmlUser))
            {
                XmlHelper xml = new XmlHelper();
                xml.LoadXml(xmlUser);
                foreach (XmlNode xuser in xml.SelectNodes("//user"))
                {
                    long user_id = long.TryParse(xml.SelectSingleNode(xuser, "id"), out user_id) ? user_id : 0;
                    string screen_name = xml.SelectSingleNode(xuser, "screen_name");
                    string name = xml.SelectSingleNode(xuser, "name");
                    string description = xml.SelectSingleNode(xuser, "description");
                    string province = xml.SelectSingleNode(xuser, "province");
                    string city = xml.SelectSingleNode(xuser, "city");
                    string location = xml.SelectSingleNode(xuser, "location");
                    string url = xml.SelectSingleNode(xuser, "url");
                    string profile_image_url = xml.SelectSingleNode(xuser, "profile_image_url");
                    string domain = xml.SelectSingleNode(xuser, "domain");
                    string gender = xml.SelectSingleNode(xuser, "gender");
                    long followers_count = long.TryParse(xml.SelectSingleNode(xuser, "followers_count"), out followers_count) ? followers_count : 0;
                    long friends_count = long.TryParse(xml.SelectSingleNode(xuser, "friends_count"), out friends_count) ? friends_count : 0;
                    long statuses_count = long.TryParse(xml.SelectSingleNode(xuser, "statuses_count"), out statuses_count) ? statuses_count : 0;
                    long favourites_count = long.TryParse(xml.SelectSingleNode(xuser, "favourites_count"), out favourites_count) ? favourites_count : 0;
                    bool following = bool.TryParse(xml.SelectSingleNode(xuser, "following"), out following) ? following : false;
                    bool verified = bool.TryParse(xml.SelectSingleNode(xuser, "verified"), out verified) ? verified : false;
                    DateTime created_at = DateTime.TryParse(StringHelper.SplitDateTime(xml.SelectSingleNode(xuser, "created_at")), out created_at) ? created_at : DateTime.Now;

                    SinaMUsers user = new SinaMUsers();
                    user.id = user_id;
                    user.screen_name = screen_name;
                    user.name = name;
                    user.description = description;
                    user.province = province;
                    user.city = city;
                    user.location = location;
                    user.url = url;
                    user.profile_image_url = profile_image_url;
                    user.domain = domain;
                    user.gender = gender;
                    user.followers_count = followers_count;
                    user.friends_count = friends_count;
                    user.statuses_count = statuses_count;
                    user.favourites_count = favourites_count;
                    user.following = following;
                    user.verified = verified;
                    user.created_at = created_at;
                    return user;
                }
            }
            return null;
        }
    }
}
